package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.WorkReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/v1/workReport")
public class WorkReportController extends BaseController {

    @Autowired
    private WorkReportService workReportService;

    /**
     * 工作报告查询（列表）
     */
    @PostMapping("/selWorkReport")
    public JsonObj selWorkReport() {
        return workReportService.selWorkReport(getPageData());
    }

    /**
     * 工作报告导出
     */
    @PostMapping("/queryAllExportData")
    public void queryAllExportData(HttpServletResponse response) {
        PageData pageData = getPageData();
        // 查询出可以用来导出的数据源
        JsonObj jsonObj = workReportService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
                .getData()
                .get("data");

        // 组装表头
        List<String> headList = Arrays.asList(
                "省份",
                "城市",
                "区县",
                "上报机构",
                "任务数",
                "自测表",
                "完成数",
                "完成率（%）",
                "病例数",
                "检出率（%）",
                "早诊数",
                "早诊率（%）",
                "治疗数",
                "治疗率（%）",
                "完成数",
                "病例数",
                "检出率（%）",
                "早诊数",
                "早诊率（%）",
                "治疗数",
                "治疗率（%）",
                "应随访数",
                "随访数",
                "随访率（%）",
                "病例数",
                "检出率（%）",
                "早诊数",
                "早诊率（%）",
                "治疗数",
                "治疗率（%）");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "工作报表", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**大屏数据*/
    @GetMapping("/getScreenData")
    public JsonObj getScreenData(@RequestParam(required = false) String year){
        return workReportService.getScreenData(year);
    }


    /**
     * 大屏数据：获取医院数据
     * 参数：年份 （year）\ 机构id（orgId）
     */
    @PostMapping("/getHospitalDataInit")
    public JsonObj getHospitalDataInit(){
        return workReportService.getHospitalDataInit(getPageData());
    }

    /**
     * 获取医院对应的城市
     */
    @GetMapping("/getCityByHospital")
    public JsonObj getCityByHospital(){
        return workReportService.getCityByHospital();
    }
}
